var ns, toast, UIActionProgress;
var app = new Vue({
    el: '#app',
    data: {
        companyName: '', //公司名称
        postionName: "", //岗位
        userHeadImg: "", //用户头像
        userName: "", //用户名称
        bannerimg: '../image/header_bg.png',
        list: [],
        notice_msg: '',
        tongzhilist: [],
        form: {
            pageindex: 0,
            pagesize: 2,
        },
        current_item_length: 0,
        is_searching: false,
        can_scroll: false,
        isRead: true,
        notifyTimeOut: null,
        Status: '',
        is_scrolled: false,
        list: [{
            "id": 686,
            "name": "区域管家",
            "url": "chooseprojectlist_frm",
            "css": "icon_sfgl",
            "title":"app_lygj_view",
            "count": 0,
            "type": 0,
            "fromtype": 4,
            "isshow": false,
            "IsAuthority": false,
        }, {
            "id": 690,
            "name": "设备巡检",
            "url": "equipment_frm",
            "css": "icon_sbxj",
            "title":"app_sbxj_view",
            "count": 0,
            "type": 0,
            "isshow": false,
            "bg": "#289FFF",
            "txtcolor": "#ffffff",
            "IsAuthority": false,
        }, {
            "id": 691,
            "name": "内部报事",
            "url": "neibubaoshi_add_frm",
            "css": "icon_nbbx",
            "title":"app_nbbs_view",
            "count": 0,
            "type": 0,
            "isshow": false,
            "IsAuthority": false,
        }, {
            "id": 692,
            "name": "安防巡更",
            "url": "patrolplan_frm",
            "css": "icon_afxg",
            "title":"app_afxg_view",
            "count": 0,
            "type": 0,
            "isshow": false,
            "bg": "#289FFF",
            "txtcolor": "#ffffff",
            "IsAuthority": false,
        }, {
            "id": 814,
            "name": "客服工单",
            "url": "customerservicelist_frm",
            "css": "icon_kfgd",
            "title":"app_kfgd_view",
            "count": 0,
            "type": 0,
            "isshow": false,
            "bg": "#289FFF",
            "txtcolor": "#ffffff",
            "IsAuthority": false,
        }, {
            "id": 995,
            "name": "设备巡检统计",
            "url": "analysis_equipment_frm",
            "css": "icon_sbtj",
            "title":"app_sbxjtj_view",
            "count": 0,
            "type": 0,
            "isshow": false,
            "IsAuthority": false,
        }, {
            "id": 996,
            "name": "安防巡更统计",
            "url": "analysis_patrolplan_frm",
            "css": "icon_aftj",
            "title":"app_afxgtj_view",
            "count": 0,
            "type": 1,
            "isshow": false,
            "IsAuthority": false,
        }, {
            "id": 999,
            "name": "考勤",
            "url": "kaoqin_frm",
            "css": "icon_kq",
            "title":"app_kq_view",
            "count": 0,
            "type": 2,
            "isshow": false,
            "IsAuthority": false,
        }
            , {
            "id": 1000,
            "name": "绩效",
            "url": "performance_frm",
            "css": "icon_jx",
            "title":"app_jx_view",
            "count": 0,
            "type": 2,
            "isshow": false,
            "IsAuthority": false,
        }, {
            "id": 1000,
            "name": "告警",
            "url": "gaojing_frm",
            "css": "icon_gj",
            "title":"app_gj_view",
            "count": 0,
            "type": 0,
            "isshow": false,
            "bg": "#289FFF",
            "txtcolor": "#ffffff",
            "IsAuthority": false,
        }],

        analysisData: {
            newCount: 0,
            closeCount: 0,
            unCloseCount: 0
        },
        scrollInterval: {
            projectInterval: null,
            msgInterval: null,
            is_scrolled: false
        },
        canViewFeeAnalysis: false,
        canViewServiceAnalysis: false,
        isedit: false,
        //我的常用
        my_use: [

        ],
        //是否在我的常用中
        isuse: false
    },
    methods: {
        get_data: function () {
            var that = this;
            var options = {};
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    that.analysisData = data.analysisData;
                    var menuArry = data.menuPowers;
                    for (var i = 0; i < that.list.length; i++) {
                        var item = that.list[i];
                        for (var j = 0; j < menuArry.length; j++) {
                            var item2 = menuArry[j];
                            if (item2 == item.title) {
                                item.isshow = true;
                                break;
                            }
                        }
                    }
                    that.get_gonggao();

                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
                // setTimeout(function () {
                //     that.get_data_count();
                //     that.get_myuse_data();
                // }, 100);
                // if (succeed) {
                //     that.canViewFeeAnalysis = data.canViewFeeAnalysis;
                //     that.canViewServiceAnalysis = data.canViewServiceAnalysis;
                //     if (data.postionName) that.postionName = data.postionName;
                //     for (var i = 0; i < that.list.length; i++) {
                //         var item = that.list[i];
                //         for (var j = 0; j < data.list.length; j++) {
                //             var item2 = data.list[j];
                //             if (item2.id == item.id) {
                //                 item.isshow = item2.isshow;
                //                 break;
                //             }
                //         }
                //     }
                // } else if (err) {
                //     api.toast({
                //         msg: err,
                //         duration: 2000,
                //         location: 'bottom'
                //     });
                // }
            }, {
                command: 'App/Home/GetData'
            });
        },
        // get_data_count: function () {
        //     var that = this;
        //     var options = {};
        //     options.action = "getmenucountlist";
        //     ns.post(options, function (succeed, data, err) {

        //         setTimeout(function () {
        //             that.getAnalysisData();
        //         }, 100);
        //         if (succeed) {
        //             for (var i = 0; i < that.list.length; i++) {
        //                 if (that.list[i].name == '设备巡检') {
        //                     that.list[i].count = data.countform.DeviceTaskCount;
        //                 }
        //                 if (that.list[i].name == '安防巡更') {
        //                     that.list[i].count = data.countform.PatrolTaskCount;
        //                 }
        //                 if (that.list[i].name == '保洁绿化') {
        //                     that.list[i].count = data.countform.EventTaskCount;
        //                 }
        //                 if (that.list[i].name == '投诉建议') {
        //                     that.list[i].count = data.countform.SuggestionCount;
        //                 }
        //                 if (that.list[i].name == '客服工单') {
        //                     that.list[i].count = data.countform.CustomerCount;
        //                 }
        //             }
        //             api.sendEvent({
        //                 name: 'isAllNumber',
        //                 extra: {
        //                     AllCount: data.countform.AllCount
        //                 }
        //             });
        //         } else if (err) {
        //             api.toast({
        //                 msg: err,
        //                 duration: 2000,
        //                 location: 'bottom'
        //             });
        //         }
        //     });
        // },
        // get_myuse_data: function () {
        //     var that = this
        //     var options = {};
        //     options.action = "getsysmenuidmodulelist";
        //     ns.post(options, function (succeed, data, err) {
        //         if (succeed) {
        //             that.my_use = []
        //             for (var i = 0; i < data.data.length; i++) {
        //                 var item = data.data[i]
        //                 for (var j = 0; j < that.list.length; j++) {
        //                     var item2 = that.list[j]
        //                     if (item2.id == item.ID) {
        //                         that.isuse = true
        //                         item2.IsAuthority = item.IsAuthority
        //                         that.my_use.push(item2)
        //                     }
        //                 }
        //             }
        //         } else if (err) {
        //             api.toast({
        //                 msg: err,
        //                 duration: 2000,
        //                 location: 'bottom'
        //             });
        //         }
        //     });
        // },
        openWin: function (item, params) {
            var that = this;
            if (item.url == '') {
                return;
            }
            params = params || {
                status: -1,
                servicetype: item.type,
                metertype: item.type,
                fromtype: item.fromtype || 0,
                selecttype: 2,
                bg: item.bg,
                txtcolor: item.txtcolor,
            };
            ns.openWin(item.url, item.name, params);
        },
        open_tongzhixiaoxi: function () {
            ns.openWin('home_news_center', '通知公告');
        },
        // open_gonggao_detail: function (id, title) {
        //     var that = this;
        //     ns.openWin('tongzhixiaoxidetail_frm', '通知公告', {
        //         id: id
        //     }, {
        //         needlogin: true
        //     });
        //     api.sendEvent({
        //         name: 'isreadtrue',
        //         extra: {
        //             key1: 'value1'
        //         }
        //     });
        // },
        get_gonggao: function () {
            var that = this;
            that.is_searching = true;
            that.can_scroll = false;
            var options = {};
            options.pageindex = that.form.pageindex;
            options.pagesize = that.form.pagesize;
            // options.action = "getmsglist";
            ns.post(options, function (succeed, data, err) {

                // setTimeout(function () {
                //     that.get_data_count();
                // }, 300);
                that.is_searching = false;
                if (succeed) {
                    that.tongzhilist = data.notifyList;
                    // that.tongzhilist=[
                    //     {id:101,title:'11你有新的工单需要处理'},
                    //     {id:102,title:'22安防巡更任务来啦'},
                    //     {id:103,title:'33你有一条告警哦'},
                    // ];
                    if (that.tongzhilist.length > 0) {
                        setTimeout(function () {
                            that.init_sroll('scrollmsgbox', 'scrollmsg');
                        }, 1000)
                    }
                } else if (err) {
                    that.current_item_length = 0;
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                command: 'App/Home/GetData'
            });
        },
        init_sroll: function (scrollElemID, leftElemID) {
            var that = this;
            var scrollOption = {};
            var scrollElem = $api.byId(scrollElemID);

            if (that.scrollInterval.is_scrolled) {
                var childNodes = scrollElem.childNodes;
                for (var i = 0; i < childNodes.length; i++) {
                    $api.remove(childNodes[i]);
                }
                if (scrollElemID == 'scrollmsgbox') {
                    if (that.scrollInterval.msgInterval) {
                        clearInterval(that.scrollInterval.msgInterval);
                        that.scrollInterval.msgInterval = null;
                    }
                }
                if (scrollElemID == 'scrollprojectbox') {
                    if (that.scrollInterval.projectInterval) {
                        clearInterval(that.scrollInterval.projectInterval);
                        that.scrollInterval.projectInterval = null;
                    }
                }
            }
            that.scrollInterval.is_scrolled = true;
            try {
                with (scrollElem) {
                    style.width = 500; //宽度
                    noWrap = true;
                }
                scrollOption.preTop = 0;
                scrollOption.currentTop = 0;
                scrollOption.stoptime = 0;
                scrollOption.scrollCount = 0;
                var leftElem = $api.byId(leftElemID);
                var childNodes = leftElem.childNodes;
                for (var i = 0; i < 2; i++) {
                    for (var j = 0; j < childNodes.length; j++) {
                        scrollElem.appendChild(childNodes[j].cloneNode(true));
                    }
                }
                // var elems = document.getElementsByName('notify_item');
                // for (var i = 0; i < elems.length; i++) {
                //     elems[i].onclick = function () {
                //         var id = $api.attr(this, 'data-id');
                //         if (scrollElemID == 'scrollmsgbox') {
                //             that.open_gonggao_detail(id);
                //         }
                //     };
                // }
            } catch (e) { }
            scrollElem.scrollTop = 0;
            if (scrollElemID == 'scrollmsgbox') {
                that.scrollInterval.msgInterval = setInterval(function () {
                    that.scroll_up(scrollElemID, leftElemID, scrollOption);
                }, 15); //滚动速度
            } else if (scrollElemID == 'scrollprojectbox') {
                that.scrollInterval.projectInterval = setInterval(function () {
                    that.scroll_up(scrollElemID, leftElemID, scrollOption);
                }, 15); //滚动速度
            }
        },
        scroll_up: function (scrollElemID, leftElemID, scrollOption) {
            var that = this;
            var scrollElem = $api.byId(scrollElemID);
            scrollOption.currentTop += 1;
            if (scrollOption.currentTop == 40) { //滚动距离56
                scrollOption.stoptime += 1;
                scrollOption.currentTop -= 1;
                if (scrollOption.stoptime == 300) { //停顿时间
                    scrollOption.currentTop = 0;
                    scrollOption.stoptime = 0;
                    scrollOption.scrollCount++;
                    if (scrollOption.scrollCount >= 10) {
                        that.init_sroll(scrollElemID, leftElemID);
                    }
                }
            } else {
                scrollOption.preTop = scrollElem.scrollTop;
                scrollElem.scrollTop += 1;
                if (scrollOption.preTop == scrollElem.scrollTop) {
                    scrollElem.scrollTop = 0;
                    scrollElem.scrollTop += 1;
                }
            }
        },
        get_user_data: function () {
            var that = this;
            // 公司名称
            var companyName = api.getPrefs({
                sync: true,
                key: 'companyName'
            });
            // 岗位名称
            var positionname = api.getPrefs({
                sync: true,
                key: 'positionname'
            });
            // 用户名称
            var username = api.getPrefs({
                sync: true,
                key: 'username'
            });
            // 用户头像
            var headimg = api.getPrefs({
                sync: true,
                key: 'headimg'
            });
            that.companyName = companyName || '重庆永友网络科技有限公司';
            that.postionName = positionname ? positionname : "";
            that.userHeadImg = headimg || '../image/default.png';
            // that.userHeadImg =  '../image/default.png';
            that.userName = username || '匿名用户';
        },
        do_open_scan_login: function (item) {
            var that = this;
            ns.confirmPer('camera', function () {
                setTimeout(function () {
                    that.do_open_scan_login_start();
                }, 200)
            })
        },
        do_open_scan_login_start: function () {
            var that = this;
            ns.openFrame('scanner_frm', {
                type: 0
            });
        },

        doOpenFeeList: function (type) {
            ns.openWin('analysis_historyfeelist_frm', '收支流水', {
                type: type
            });
        },
        doOpenServiceSummary: function (type) {
            ns.openWin('analysis_customerservice_summary_frm', '客服工单统计');
        },
        // doOpenServiceList: function(type) {
        //     ns.openWin('customerservicelist_tongji', '客服工单统计', {
        //         isviewdetail: true,
        //         "bg":"#289FFF",
        //         "txtcolor":"#ffffff"
        //     });
        // },
        // getAnalysisData: function () {
        //     var that = this;
        //     if (!that.canViewFeeAnalysis && !that.canViewServiceAnalysis) {
        //         return;
        //     }
        //     var options = {
        //         action: 'gethomeanalysisdata'
        //     };
        //     ns.post(options, function (succeed, data, err) {
        //         if (succeed) {
        //             that.summary = data;
        //             if (that.summary.projectFeeList && that.summary.projectFeeList.length > 0) {
        //                 setTimeout(function () {
        //                     that.init_sroll('scrollprojectbox', 'scrollprojectmsg');
        //                 }, 3000)
        //             }
        //         } else if (err) {
        //             api.toast({
        //                 msg: err,
        //                 duration: 2000,
        //                 location: 'bottom'
        //             });
        //         }
        //     });
        // },
        // 打开个人中心
        openSetting: function () {
            ns.openWin('gerenzhongxin_frm', '个人信息');
        },
        // do_edit: function () {
        //     var that = this;
        //     that.isedit = !that.isedit;
        //     if (that.isedit == false) {
        //         var sss = []
        //         that.my_use.forEach(function (item) {
        //             if (item.IsAuthority == true) {
        //                 sss.push(item.id)
        //             }
        //         })
        //         var options = {}
        //         options.action = 'savecommonmodule';
        //         options.SysMenuIds = JSON.stringify(sss)
        //         ns.post(options, function (succeed, data, err) {
        //             if (succeed) {
        //                 api.toast({
        //                     msg: '保存成功',
        //                     duration: 2000,
        //                     location: 'bottom'
        //                 });
        //             } else if (err) {
        //                 api.toast({
        //                     msg: err,
        //                     duration: 2000,
        //                     location: 'bottom'
        //                 });
        //             }
        //         },
        //             {
        //                 toast: true
        //             });
        //     }

        // },
        //添加我的常用
        add_my_use: function (item) {
            var that = this;
            that.isuse = true
            var myuse = {}
            Object.assign(myuse, item)
            if (myuse.id == item.id) {
                item.IsAuthority = true
                myuse.IsAuthority = item.IsAuthority

            }
            that.my_use.push(myuse)
        },
        //删除我的常用
        delete_my_use: function (item) {
            var that = this;
            item.IsAuthority = false
            for (i = 0; i < that.list.length; i++) {
                if (item.id == that.list[i].id) {
                    that.list[i].IsAuthority = item.IsAuthority
                }
            }
            for (var i = 0; i < that.my_use.length; i++) {
                var item2 = that.my_use[i]
                if (item2.id == item.id) {
                    that.my_use.splice(i, 1)
                }
            }
            if (that.my_use.length == 0) {
                that.isuse = false
            }
        },
        set_status_bar: function () {
            var that = this;
            api.setStatusBarStyle({
                style: 'dark',
                color: 'transparent'
            });
            api.closeFrame({
                name: 'main_login_win'
            });
        }
    }
});
apiready = function () {
    api.parseTapmode();
    setTimeout(function () {
        app.set_status_bar();
    }, 2000)
    ns = window.Foresight.Util;
    UIActionProgress = api.require('UIActionProgress');
    toast = new auiToast();
    app.get_data();
    // app.getAnalysisData();
    // setTimeout(function () {
    //     appupgrade.check_upgrade(false);
    // }, 5000)
    api.addEventListener({
        name: 'onlogin'
    }, function (ret, err) {
        app.set_status_bar();
    });
    ns.init_pull(true);
    app.get_user_data();
    api.addEventListener({
        name: 'changecentersuccess'
    }, function () {
        app.get_user_data();
    });
    api.addEventListener({
        name: 'home_new_updata'
    }, function () {
        app.get_data();
    });
    // api.addEventListener({
    //     name: 'purchase_updata'
    // }, function () {
    //     app.get_data_count();
    // });
    // api.addEventListener({
    //     name: 'reload_Analysis'
    // }, function () {
    //     app.getAnalysisData();
    // });
    // api.addEventListener({
    //     name: 'reloadservicelist'
    // }, function (ret, err) {
    //     app.get_data_count();
    // });
    api.addEventListener({
        name: 'isreadtrue'
    }, function (ret, err) {
        if (ret) {
            setTimeout(function () {
                app.get_data();
            }, 500);
        } else {
            api.toast({
                msg: err,
                duration: 2000,
                location: 'bottom'
            });
        }
    });
}
